#ifndef _AVR_TWI_H_
#define _AVR_TWI_H_

#include "../../Component.h"
#include<avr/io.h>
#include<avr/interrupt.h>
#include "../../../bit/bit.h"

namespace Hardware
{
namespace Avr
{

	typedef struct TWI_CONFIG
	{
		unsigned char bitRate;
		unsigned char enableAck : 1;
		unsigned char start : 1;
		unsigned char stop : 1;
		unsigned char enable : 1;
		unsigned char interrupt : 1;
		unsigned char prescale : 2;
		unsigned char callRecognition : 1;
		
	} TwiConfig;

	typedef struct TWI_DATA
	{
		unsigned char status : 5;
		unsigned char data;
		unsigned char address;
	} TwiData;

	class Twi : public Component<TwiConfig, TwiData>
	{
		public :

		Twi(){}
		Twi(TwiConfig c){configuration=c;}
		void config()
		{
			//set the bit rate
			TWBR = configuration.bitRate;
			//set acknowledge
			TWCR = SetBit(TWCR, TWEA, configuration.enableAck);
			//set start
			TWCR = SetBit(TWCR, TWSTA, configuration.start);
			//set stop
			TWCR = SetBit(TWCR, TWSTO, configuration.stop);
			//set enable
			TWCR = SetBit(TWCR, TWEN, configuration.enable);
			//set interrupt
			TWCR = SetBit(TWCR, TWIE, configuration.interrupt);
			//get status
			data.status = TWSR >> 3;
			//get prescaler
			TWSR = (TWSR & ~0x03) | configuration.prescale;
			//set call recognition 
			TWAR = SetBit(TWAR, TWGCE, configuration.callRecognition);
		}
		void run(){}
	};
}
}




#endif
